package com.bainuo.doctor.common.util;

import android.os.Handler;


/**
 * Created by SeenFar on 16/5/31.
 */
public class MyUtil {

    public static class LoopHandler extends Handler {
        public Runnable runnable;

        public void stop() {
            if (runnable != null) {
                removeCallbacks(runnable);
                runnable = null;
            }
        }

    }

    public static LoopHandler runLoop(int time, Loop loop) {

        LoopHandler handler = new LoopHandler();
        postDelayed(handler, time, loop);
        return handler;
    }


    public static void postDelayed(final LoopHandler handler, final int time, final Loop loop) {

        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (loop != null && loop.doexec()) {
                    postDelayed(handler, time, loop);
                }
            }
        };
        handler.runnable = runnable;
        handler.postDelayed(runnable, time);
    }

    public interface Loop {
        boolean doexec();
    }



}
